using AspectCore.DynamicProxy;
using Microsoft.Extensions.Caching.Memory;

namespace SN.WebApi.AOP;

public class MemercacheFilter : AbstractInterceptorAttribute
{
    static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());

    public override async Task Invoke(AspectContext context, AspectDelegate next){
        try{
            System.Console.WriteLine("执行之前");
            var res = _memoryCache.Get("user");
            if(res == null){
                await next(context);
                _memoryCache.Set("user", context.ReturnValue,DateTime.Now.AddMinutes(5));
                System.Console.WriteLine("缓存数据");
            }
            else{
                System.Console.WriteLine("从缓冲中读取数据");
            }
        }catch{
            System.Console.WriteLine("拦截的方法出现异常");
        }
        finally{

        }
    }
}
